package cn.fanqinghong.org.service.impl;

import cn.fanqinghong.basic.service.impl.BaseServiceImpl;
import cn.fanqinghong.org.domain.Employee;
import cn.fanqinghong.org.mapper.EmployeeMapper;
import cn.fanqinghong.org.service.IEmployeeService;
import cn.fanqinghong.user.domain.Logininfo;
import cn.fanqinghong.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * FAN
 * 2022/6/1  11:54
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    public void save(Employee employee) {
        Logininfo logininfo = emp2Logininfo(employee);
        //保存logininfo
        logininfoMapper.save(logininfo);//这里要返回自增的id
        //使用自增id设置employee中的Logininfo_id
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);
    }

    /**
     * 设置Logininfo中和emplyee相同的的参数
     * @param employee
     * @return
     */
    private Logininfo emp2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//这里设置的后端管理
        return logininfo;
    }

    /**
     * 通过当前登陆人查询到员工的数据
     * @param id
     * @return
     */
    @Override
    public Employee loadByLogininfoId(Long id) {
        return employeeMapper.loadByLogininfoId(id);
    }

    @Override
    public List<Employee> queryByShopEmplyess(Long shopId) {
        return employeeMapper.loadByShopEmplyess(shopId);
    }
}
